//
//  YRSandBox.swift
//  YRSandBox
//
//  Created by 薛永伟 on 2019/5/31.
//  Copyright © 2019 薛永伟. All rights reserved.
//

import Foundation

open class YRSandBox:NSObject {
    
    
    /// 根目录
    public static let homeDirectory = NSHomeDirectory()
    
    /// 获取Documents目录路径（保存应用程序的重要数据文件和用户数据文件等）
    public static let docDirectory = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, .userDomainMask, true).first!
    
    /// 获取Library的目录路径（下有Caches和Preferences文件夹。用来放置您希望被备份但不希望被用户看到的数据）
    public static let libDirectory = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.libraryDirectory, .userDomainMask, true).first!
    
    // 获取cache目录路径（保存应用程序再次启动过程中需要的信息，可能被工具清理掉）
    public static let cacheDirectory = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.cachesDirectory, .userDomainMask, true).first!
    
    // 获取preference目录路径（保存应用程序的偏好设置文件）
    public static let preferenceDirectory = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.preferencePanesDirectory, .userDomainMask, true).first!
    
    // 获取temp目录路径(重启设备清空)
    public static let tempDirectory = NSTemporaryDirectory()
    
    
    public
    struct Group {
        
        static let identifier = "group.g.com.hlxmf.superrecoder"
    }
    
    
}

//MARK: - --------------YRSandBox.Group--------------
extension YRSandBox.Group {
    /// 应用组文件夹地址
    
    public static var groupDirectoryPath: String = {
        let sharePath = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.g.com.hlxmf.superrecoder")!.path
        return sharePath
    }()
    
    /// 视频文件夹路径
    public static var vodeosDirectoryPath: String = {
        
        let dir = groupDirectoryPath + "/videos"
        if FileManager.default.fileExists(atPath: dir, isDirectory: nil){
            return dir
        }else{
            do {
                try FileManager.default.createDirectory(atPath: dir, withIntermediateDirectories: true, attributes: nil)
            }catch{
                print(error.localizedDescription)
            }
        }
        return dir
    }()
    
    /// 暂存文件夹
    public static var tempDirectoryPath: String = {
        
        return groupDirectoryPath + "/temps"
    }()
}


//MARK: - --------------YRSandBox.Group辅助方法--------------

extension YRSandBox.Group {
    
    /// 创建一个以现在时间命名的文件名
    static func createFileNameByNow()->String{
        
        
        let dataFormater = DateFormatter.init()
        dataFormater.dateFormat = "yyyyMMdd-HHmmss"
        let now = Date()
        let result = dataFormater.string(from: now)
        return result
    }
    
    /// 创建一个以现在时间命名的字符串
    static func timeStringByNow()-> String {
        
        
        let dataFormater = DateFormatter.init()
        dataFormater.dateFormat = "yyyyMMddHHmmss"
        let now = Date()
        let result = dataFormater.string(from: now)
        return result
    }
    
    public static func createNewMovieDirectory() -> String {
        
        let now = timeStringByNow()
        let dir = groupDirectoryPath + "/videos/\(now)"
        
        if FileManager.default.fileExists(atPath: dir, isDirectory: nil){//如果已经有了，将覆盖原来的文件
            return dir
        }else{
            do {
                try FileManager.default.createDirectory(atPath: dir, withIntermediateDirectories: true, attributes: nil)
            }catch{
                print(error.localizedDescription)
            }
        }
        return dir
    }
    
    public static func allVideoPaths() -> [String]? {
        
        do {
            let paths = try FileManager.default.contentsOfDirectory(atPath: vodeosDirectoryPath)
            return paths.map({return "\(vodeosDirectoryPath)/\($0)"})
//            return paths
        }catch {
            print("出错了:\(error.localizedDescription)")
            return nil
        }
        
    }
}


extension YRSandBox {
    
    /// 录制器
    public struct Recoder {
        
        /// 新文件路径
        public static func newMp4FilePath() -> String {
            
            let newName = Group.createFileNameByNow()
            let path = Group.vodeosDirectoryPath+"/"+newName + ".mp4"
            return path
        }
    }
}
